import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MainFrame extends JFrame {

	private static final long serialVersionUID = 1L;

	private Conteneur panneau = new Conteneur();

	private File background_f = new File("Graphisme/Interface_principale/background.png");
	private File icone_f = new File("Graphisme/icone.png");
	private File button_f = new File("Graphisme/Interface_principale/button.png");
	private File cadre_gd_avatar_f = new File("Graphisme/Interface_principale/cadre_grand_avatar.png");
	private File logo_f = new File("Graphisme/Interface_principale/logo.png");
	private File new_tweet_f = new File("Graphisme/Interface_principale/new_tweet.png");
	private File research_n_f = new File("Graphisme/Interface_principale/research_n.png");
	private File research_h_f = new File("Graphisme/Interface_principale/research_h.png");
	private File search_bar_f = new File("Graphisme/Interface_principale/search_bar.png");
	private File separation_f = new File("Graphisme/Interface_principale/separation.png");
	private File tweet_box_f = new File("Graphisme/Interface_principale/tweet_box.png");

	private File dm_h_f = new File("Graphisme/Interface_principale/menu/dm_h.png");
	private File dm_n_f = new File("Graphisme/Interface_principale/menu/dm_n.png");
	private File fave_h_f = new File("Graphisme/Interface_principale/menu/fave_h.png");
	private File fave_n_f = new File("Graphisme/Interface_principale/menu/fave_n.png");
	private File interaction_h_f = new File("Graphisme/Interface_principale/menu/interaction_h.png");
	private File interaction_n_f = new File("Graphisme/Interface_principale/menu/interaction_n.png");
	private File list_h_f = new File("Graphisme/Interface_principale/menu/list_h.png");
	private File list_n_f = new File("Graphisme/Interface_principale/menu/list_n.png");
	private File mention_h_f = new File("Graphisme/Interface_principale/menu/mention_h.png");
	private File mention_n_f = new File("Graphisme/Interface_principale/menu/mention_n.png");
	private File timeline_h_f = new File("Graphisme/Interface_principale/menu/timeline_h.png");
	private File timeline_n_f = new File("Graphisme/Interface_principale/menu/timeline_n.png");
	private File trend_n_f = new File("Graphisme/Interface_principale/menu/trend_n.png");
	private File trend_h_f = new File("Graphisme/Interface_principale/menu/trend_h.png");
	private File tweet_n_f = new File("Graphisme/Interface_principale/menu/tweet_n.png");
	private File tweet_h_f = new File("Graphisme/Interface_principale/menu/tweet_h.png");

	private File answer_h_f = new File("Graphisme/Interface_principale/tweet_button/answer_h.png");
	private File answer_n_f = new File("Graphisme/Interface_principale/tweet_button/answer_n.png");
	private File delete_h_f = new File("Graphisme/Interface_principale/tweet_button/delete_h.png");
	private File delete_n_f = new File("Graphisme/Interface_principale/tweet_button/delete_n.png");
	private File fav_h_f = new File("Graphisme/Interface_principale/tweet_button/fav_h.png");
	private File fav_n_f = new File("Graphisme/Interface_principale/tweet_button/fav_n.png");
	private File retweet_h_f = new File("Graphisme/Interface_principale/tweet_button/retweet_h.png");
	private File retweet_n_f = new File("Graphisme/Interface_principale/tweet_button/retweet_n.png");
	private File save_h_f = new File("Graphisme/Interface_principale/tweet_button/save_h.png");
	private File save_n_f = new File("Graphisme/Interface_principale/tweet_button/save_n.png");

	private JButton research_b = new JButton();
	private JButton dm_b = new JButton();
	private JButton fave_b = new JButton();
	private JButton interaction_b = new JButton();
	private JButton list_b = new JButton();
	private JButton mention_b = new JButton();
	private JButton timeline_b = new JButton();
	private JButton trend_b = new JButton();
	private JButton tweet_b = new JButton();
	private JButton answer_b = new JButton();
	private JButton delete_b = new JButton();
	private JButton fav_b = new JButton();
	private JButton retweet_b = new JButton();
	private JButton save_b = new JButton();
	private JButton refresh_b = new JButton();
	private JButton deco_b = new JButton();
	private JButton parametre_b = new JButton();
	private JButton reduce_b = new JButton();
	// private JButton max_b = new JButton();
	private JButton close_b = new JButton();
	private JButton post_tweet = new JButton();

	private JLabel label = new JLabel();
	private boolean warning_b = false;

	public MainFrame(final Client twitter) {
		this.setSize(994, 749);
		this.setLocationRelativeTo(null);
		// this.addMouseListener(new gestionSouris());

		Image icone;
		try {
			icone = ImageIO.read(icone_f);
			this.setIconImage(icone);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		this.setUndecorated(true);

		panneau.setLayout(null);

		try {
			/* 0 */ComposantGraphique background = new ComposantGraphique(background_f, 0, 0);
			/* 1 */ComposantGraphique button = new ComposantGraphique(button_f,789, 0);
			/* 2 */ComposantGraphique cadre_gd_avatar = new ComposantGraphique(cadre_gd_avatar_f, 297, 21);
			/* 3 */ComposantGraphique logo = new ComposantGraphique(logo_f, 0, 0);
			/* 4 */ComposantGraphique new_tweet = new ComposantGraphique(new_tweet_f, 280, 643);
			/* 5 */ComposantGraphique research = new ComposantGraphique(research_n_f, 235, 673);
			/* 6 */ComposantGraphique search_bar = new ComposantGraphique(search_bar_f, 3, 617);
			/* 7 */ComposantGraphique tweet_box = new ComposantGraphique(tweet_box_f, 18, 171);
			/* 8 */ComposantGraphique dm = new ComposantGraphique(dm_n_f, 867, 649);
			/* 9 */ComposantGraphique fave = new ComposantGraphique(fave_n_f, 822, 687);
			/* 10 */ComposantGraphique interaction = new ComposantGraphique(interaction_n_f, 924, 644);
			/* 11 */ComposantGraphique list = new ComposantGraphique(list_n_f, 920, 690);
			/* 12 */ComposantGraphique mention = new ComposantGraphique(mention_n_f, 825, 645);
			/* 13 */ComposantGraphique timeline = new ComposantGraphique(timeline_n_f, 779, 645);
			/* 14 */ComposantGraphique trend = new ComposantGraphique(trend_n_f, 866, 688);
			/* 15 */ComposantGraphique tweet = new ComposantGraphique(tweet_n_f, 777, 689);

			panneau.ajouterComposant(background);
			panneau.ajouterComposant(button);
			panneau.ajouterComposant(cadre_gd_avatar);
			panneau.ajouterComposant(logo);
			panneau.ajouterComposant(new_tweet);
			panneau.ajouterComposant(research);
			panneau.ajouterComposant(search_bar);
			panneau.ajouterComposant(tweet_box);
			panneau.ajouterComposant(dm);
			panneau.ajouterComposant(fave);
			panneau.ajouterComposant(interaction);
			panneau.ajouterComposant(list);
			panneau.ajouterComposant(mention);
			panneau.ajouterComposant(timeline);
			panneau.ajouterComposant(trend);
			panneau.ajouterComposant(tweet);

			panneau.repaint();

		} catch (IOException e) {
			e.printStackTrace();
		}

		/****** Bouton ******/

		research_b.setOpaque(false);
		research_b.setContentAreaFilled(false);
		research_b.setBorderPainted(false);
		research_b.setBounds(235, 673, 31, 35);
		research_b.setToolTipText("Launch search");
		research_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(5, research_h_f, 235, 673);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(5, research_n_f, 235, 673);
				panneau.repaint();
			}
		});

		dm_b.setOpaque(false);
		dm_b.setContentAreaFilled(false);
		dm_b.setBorderPainted(false);
		dm_b.setBounds(867, 649, 34, 21);
		dm_b.setToolTipText("Direct messages");
		dm_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(8, dm_h_f, 867, 649);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(8, dm_n_f, 867, 649);
				panneau.repaint();
			}
		});

		fave_b.setOpaque(false);
		fave_b.setContentAreaFilled(false);
		fave_b.setBorderPainted(false);
		fave_b.setBounds(822, 697, 29, 27);
		fave_b.setToolTipText("Favorites");
		fave_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(9, fave_h_f, 822, 687);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(9, fave_n_f, 822, 687);
				panneau.repaint();
			}
		});

		interaction_b.setOpaque(false);
		interaction_b.setContentAreaFilled(false);
		interaction_b.setBorderPainted(false);
		interaction_b.setBounds(924, 644, 32, 29);
		interaction_b.setToolTipText("Interactions");
		interaction_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(10, interaction_h_f, 924, 644);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(10, interaction_n_f, 924, 644);
				panneau.repaint();
			}
		});

		list_b.setOpaque(false);
		list_b.setContentAreaFilled(false);
		list_b.setBorderPainted(false);
		list_b.setBounds(920, 690, 37, 25);
		list_b.setToolTipText("List");
		list_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(11, list_h_f, 920, 690);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(11, list_n_f, 920, 690);
				panneau.repaint();
			}
		});

		mention_b.setOpaque(false);
		mention_b.setContentAreaFilled(false);
		mention_b.setBorderPainted(false);
		mention_b.setBounds(825, 645, 26, 28);
		mention_b.setToolTipText("Mentions");
		mention_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(12, mention_h_f, 825, 645);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(12, mention_n_f, 825, 645);
				panneau.repaint();
			}
		});

		timeline_b.setOpaque(false);
		timeline_b.setContentAreaFilled(false);
		timeline_b.setBorderPainted(false);
		timeline_b.setBounds(779, 645, 27, 27);
		timeline_b.setToolTipText("Timeline");
		timeline_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(13, timeline_h_f, 779, 645);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(13, timeline_n_f, 779, 645);
				panneau.repaint();
			}
		});

		trend_b.setOpaque(false);
		trend_b.setContentAreaFilled(false);
		trend_b.setBorderPainted(false);
		trend_b.setBounds(866, 688, 38, 30);
		trend_b.setToolTipText("Trends");
		trend_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(14, trend_h_f, 866, 688);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(14, trend_n_f, 866, 688);
				panneau.repaint();
			}
		});

		tweet_b.setOpaque(false);
		tweet_b.setContentAreaFilled(false);
		tweet_b.setBorderPainted(false);
		tweet_b.setBounds(777, 689, 30, 26);
		tweet_b.setToolTipText("Tweets");
		tweet_b.addMouseListener(new MouseAdapter() {
			public void mouseEntered(MouseEvent e) {
				panneau.modifComposant(15, tweet_h_f, 777, 689);
				panneau.repaint();
			}

			public void mouseExited(MouseEvent e) {
				panneau.modifComposant(15, tweet_n_f, 777, 689);
				panneau.repaint();
			}
		});

		refresh_b.setOpaque(false);
		refresh_b.setContentAreaFilled(false);
		refresh_b.setBorderPainted(false);
		refresh_b.setBounds(804, 3, 16, 18);
		refresh_b.setToolTipText("Refresh");

		deco_b.setOpaque(false);
		deco_b.setContentAreaFilled(false);
		deco_b.setBorderPainted(false);
		deco_b.setBounds(831, 8, 25, 11);
		deco_b.setToolTipText("Sign out");
		deco_b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				twitter.destroySession();
				@SuppressWarnings("unused")
				ConnexionFrame accueil = new ConnexionFrame();
				dispose();
			}
		});

		parametre_b.setOpaque(false);
		parametre_b.setContentAreaFilled(false);
		parametre_b.setBorderPainted(false);
		parametre_b.setBounds(874, 2, 26, 17);
		parametre_b.setToolTipText("Parameters");

		reduce_b.setOpaque(false);
		reduce_b.setContentAreaFilled(false);
		reduce_b.setBorderPainted(false);
		reduce_b.setBounds(905, 16, 13, 4);
		reduce_b.setToolTipText("Reduce");
		reduce_b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				setExtendedState(JFrame.ICONIFIED);
			}
		});

		close_b.setOpaque(false);
		close_b.setContentAreaFilled(false);
		close_b.setBorderPainted(false);
		close_b.setBounds(966, 7, 11, 12);
		close_b.setToolTipText("Quit");
		close_b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				System.exit(0);
			}
		});

		/**** Post tweet ****/
		
		label.setBounds(630, 700, 30, 20);
		label.setFont(new Font("Verdana", 1, 10));
		label.setForeground(new Color(6, 122, 127));
				
		final JTextField warning = new JTextField("Your tweet is too long !");
		warning.setEditable(false);
		warning.setBounds(400, 700, 150, 20);
		warning.setBorder(BorderFactory.createEmptyBorder());
		warning.setFont(new Font("Verdana", 1, 10));
		warning.setForeground(new Color(6, 122, 127));
		warning.setBackground(new Color(255, 255, 255));
		
		final JTextField tweet_status = new JTextField();
		tweet_status.setEditable(false);
		tweet_status.setBounds(400, 700, 150, 20);
		tweet_status.setBorder(BorderFactory.createEmptyBorder());
		tweet_status.setFont(new Font("Verdana", 1, 10));
		tweet_status.setForeground(new Color(6, 122, 127));
		tweet_status.setBackground(new Color(255, 255, 255));
		
		final JTextArea post_tweet_jta = new JTextArea(2, 25);
		post_tweet_jta.setBounds(300, 655, 360, 50);
		post_tweet_jta.setFont(new Font("Verdana", 1, 12));
		post_tweet_jta.setBorder(BorderFactory.createEmptyBorder());
		post_tweet_jta.setLineWrap(true);
		post_tweet_jta.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				label.setText("" + (140 - (post_tweet_jta.getText().length())));
				if (post_tweet_jta.getText().length() > 140)
				{
					warning_b = true;
					panneau.add(warning);
					panneau.repaint();					
				}
				else if (post_tweet_jta.getText().length() <= 140 && warning_b)
				{
					panneau.remove(warning);
					panneau.repaint();
				}

			}
		});

		post_tweet.setOpaque(false);
		post_tweet.setContentAreaFilled(false);
		post_tweet.setBorderPainted(false);
		post_tweet.setBounds(659, 654, 62, 49);
		post_tweet.setToolTipText("Tweet this");
		post_tweet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (post_tweet_jta.getText().length() <= 140) {
					
					tweet_status.setText(twitter.sendTweet(post_tweet_jta.getText()));
					
					try {
						Thread.sleep(5000);
						post_tweet_jta.setText("");
						label.setText("" + (140 - (post_tweet_jta.getText().length())));
						//tweet_status.setText("");
						
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			}
		});

		panneau.add(close_b);
		panneau.add(deco_b);
		panneau.add(dm_b);
		panneau.add(fave_b);
		panneau.add(interaction_b);
		panneau.add(list_b);
		panneau.add(mention_b);
		panneau.add(parametre_b);
		panneau.add(label);
		panneau.add(tweet_status);
		panneau.add(post_tweet_jta);
		panneau.add(post_tweet);
		panneau.add(reduce_b);
		panneau.add(refresh_b);
		panneau.add(research_b);
		panneau.add(timeline_b);
		panneau.add(trend_b);
		panneau.add(tweet_b);

		this.setContentPane(panneau);
		this.setVisible(true);

	}
}
